package com.wangle.servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pro.bean.Card_Group;
import com.pro.bean.Groups;
import com.wangle.dao.GroupImpl;
import com.dao.ObjectDao;


public class GroupServlet extends HttpServlet {
 
	public GroupServlet(){
		super();
	}
	
	public void init() throws ServletException {
		// Put your code here
	}
	

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Groups groupp =new Groups();
		ObjectDao ObjectDao2=new ObjectDao();
		List lUnit=ObjectDao2.findall(groupp);
		Set<Integer> set = new HashSet<Integer>();
		Iterator it = lUnit.iterator();
		List rs = new ArrayList();
		while(it.hasNext())
		{
			Groups g = (Groups)it.next();
			if( set.add(g.getPrgroupid()) )
			{
				System.out.println(g.getPrgroupid());
				rs.add(g);
			}
		}
		request.getSession().setAttribute("list", rs);
		
		
		 System.out.println("��ѯ����");//��ѯ��
         Groups group=new Groups();
		 GroupImpl groupi=new GroupImpl();
    	 request.getSession().setAttribute("group", group);
    	 
    	 
		 ArrayList<Groups> groups=groupi.query(group,true);//�����ȫ����ѯ����true,����false	 
		 request.getSession().setAttribute("groups", groups);//����ѯ����ǰ̨
		
		 
		 request.getRequestDispatcher("jsp/groupLeft.jsp").forward(request, response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		 if(request.getParameter("status")!=null && request.getParameter("status").equals("addJsp")){
			   System.out.println("1��ӷ��飡����");
			    Groups group=new Groups();
				GroupImpl groupi=new GroupImpl();	
				group.setGroupname(request.getParameter("groupnameInAdd"));
				group.setPrgroupid(Integer.valueOf(request.getParameter("fathetgroupInAdd")));
				group.setGroupnote(request.getParameter("groupnoteInAdd"));
				groupi.save(group);//���
				ArrayList<Groups> groups=groupi.query(group,true);//���²�ѯ�������	
				request.getSession().setAttribute("group", group);
				request.getSession().setAttribute("allQuery", true);

			    //����ӷ����ѯ����ǰ̨,��Ϊ����ˢ����ҳ�棬���Բ����ٴη���ǰ̨
			     request.setAttribute("groups", groups);
			    request.getRequestDispatcher("jsp/groupLeft.jsp").forward(request,response);
		    }else if(request.getParameter("status")!=null && request.getParameter("status").equals("deleteGroup")){
				System.out.println("2ɾ��һ������");
				Groups group=new Groups();
				GroupImpl groupi=new GroupImpl();	
				group.setGroupid(Integer.valueOf(request.getParameter("groupid")));
				groupi.delete(group);	
				//ArrayList<Groups> groups=groupi.query(group,true);	
				//request.getSession().setAttribute("group", group);
				//request.getSession().setAttribute("allQuery", true);


			}else if(request.getParameter("status")!=null && request.getParameter("status").equals("reJsp")){
				 System.out.println("3���·��飡����");
			Groups group=new Groups();
			GroupImpl groupi=new GroupImpl();

			int groupid=Integer.valueOf(request.getParameter("groupid"));
			request.setAttribute("groupid", groupid);
			group.setGroupid(groupid);
			group.setGroupname(request.getParameter("groupname"));
			group.setGroupnote(request.getParameter("groupnote"));
			group.setPrgroupid(Integer.parseInt(request.getParameter("prgroupid")));
			groupi.update(group);
			//ArrayList<Groups> groups=groupi.query(group,true);//���²�ѯ�������	
			//request.getSession().setAttribute("group", group);
			//request.getSession().setAttribute("allQuery", true);

			//request.getRequestDispatcher("jsp/groupLeft.jsp").forward(request, response);
		}
		
	}

	
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

}
